%p Tutorial 2 : SCOPES:

%p scopes are used to define the entries to execute the queries on. Assume that I know that all the queries I write have the condition BlogPost.excludes(title: "untitled") because I want to search in the posts with title named "titled" only. In this case I can define a new scope for the posts with title = "titled" using: scope :titledPosts,excludes(title: "untitled") in the Model. From now on, in stead of writing BlogPost, I will write BlogPost.titledPosts to give all all entries in this scope. I can then perform all the operations we talked about on it.

= @blogs = BlogPost.titledPosts


- @blogs.each do |blog_post|
	%p
		%b= link_to blog_post.title, blog_post 
	%p created at
	%p= blog_post.time
	%p Tags:
	%p= blog_post.tag1
	%p= blog_post.tag2
	%p= blog_post.tag3
	%p Content:
	%p= blog_post.content
	%p id:	
	%p= blog_post.id

	%p{style: "float: right;"}
		= link_to "Delete", blog_post, method: :delete, confirm: "Are you sure you want to delete this post?"

		= link_to "modify", blog_post, method: :put
	%hr{style: "clear: both;"}

%p 
	As you can see, only blogs in the scope are selected. I can mix scopes as BlogPost.titledPosts.atag (here atag and current are another scopes I defined.) The next lins show the result for BlogPost.titledPosts.current (check the definition of these scopes in the model)
	Note#1: class methods that return criteria can be treated as chainable scopes as well. (BlogPost.nameOfMethod.(what you want to do with the return value of the method))
	Note#2: Named scopes and class methods that return a criteria can be chained together - that's the beauty of Mongoid's powerful criteria API. 
	%br 
	
= @blogsScope = BlogPost.titledPosts.current


- @blogsScope.each do |blog_post|
	%p
		%b= link_to blog_post.title, blog_post 
	%p created at
	%p= blog_post.time
	%p Tags:
	%p= blog_post.tag1
	%p= blog_post.tag2
	%p= blog_post.tag3
	%p Content:
	%p= blog_post.content
	%p id:	
	%p= blog_post.id

	%p{style: "float: right;"}
		= link_to "Delete", blog_post, method: :delete, confirm: "Are you sure you want to delete this post?"

		= link_to "modify", blog_post, method: :put
	%hr{style: "clear: both;"}

%p
	Note: default scopes
	You can supply a default scope at the class level to tell Mongoid that all queries need to filter by the provided criteria without needing to 	specify it for every query. If I define default_scope where(title: "sport"), all the queries I perform will search on only the entries with title= "sport" without a need to explicitly say so because it is a default scope.

	When a default scope is defined, you may find yourself executing queries that do not need the default applied. This is where Mongoid's familiar unscoped method comes in.

	We just want some current posts who aren't entitled "sport".
	Person.unscoped.current


= link_to 'Back', blog_posts_path

